package com.example.demo2.controller;

import com.example.demo2.bean.FragmentBean;
import com.example.demo2.service.FragmentBeanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*",
        methods = {RequestMethod.GET, RequestMethod.OPTIONS, RequestMethod.POST})
public class FragmentBeanController {
    @Autowired
    FragmentBeanService yourBeanService;

//    // 登录
//    @RequestMapping("/YourServer/fragmentBean/login")
//    public Map<String, Object> getMember(@RequestBody FragmentBean yourBean) {
//        Map<String, Object> map = new HashMap<>();
//        FragmentBean yourBean1 = yourBeanService.login(yourBean);
//        if (yourBean1 != null) {
//            map.put("success", true);
//            map.put("msg", "成功");
//        } else {
//            map.put("success", false);
//            map.put("msg", "失败");
//        }
//        return map;
//    }
//
    // 标签
    @RequestMapping("/fragment/lab")
    public Map<String, Object> addMember() {
        Map<String, Object> map = new HashMap<>();
        String[] lable = {"笔记","日记","伤感","娱乐","愿望","游戏","攻略","王者","吃鸡"};
            map.put("success", true);
            map.put("msg", "成功");
            map.put("code", 0);
            map.put("lab",lable);
        return map;
    }

    // 查询一个
    @RequestMapping("/fragmentBean/findone")
    public Map<String, Object> findone(@RequestBody FragmentBean yourBean) {
        Map<String, Object> map = new HashMap<>();
        FragmentBean yourBean1 = yourBeanService.findone(yourBean);
        if (yourBean1 != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("code", 0);
            map.put("data", yourBean1);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
            map.put("code", 1);
        }
        return map;
    }
//
//    // 查询所有
    @RequestMapping("/fragmentBean/list")
    public Map<String, Object> list() {
        Map<String, Object> map = new HashMap<>();
//        String[] lable = {"笔记","日记","伤感","娱乐","愿望","游戏","攻略","王者","吃鸡"};
        List<FragmentBean> list = yourBeanService.list();
        if (list != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("code", 0);
//            map.put("lab",lable);
            map.put("list", list);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
            map.put("code", 1);
        }
        return map;
    }
//
//    // 删除
    @RequestMapping("/fragmentBean/delete")
    public Map<String, Object> delete(@RequestBody FragmentBean yourBean) {
        Map<String, Object> map = new HashMap<>();
        try {
            yourBeanService.deleteById(yourBean.getFg_id());
            map.put("success", true);
            map.put("msg", "成功");
            map.put("code", 0);
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg", "失败");
            map.put("code", 1);

        }

        return map;
    }
    // 修改
    @RequestMapping("/fragmentBean/update")
    public Map<String, Object> update(@RequestBody FragmentBean yourBean) {
        Map<String, Object> map = new HashMap<>();
        FragmentBean yourBean1 = yourBeanService.update(yourBean);
        if (yourBean1 != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("code", 0);
            map.put("data", yourBean1);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
            map.put("code", 1);
        }
        return map;
    }
    // 添加
    @RequestMapping("/fragmentBean/addone")
    public Map<String, Object> addone(@RequestBody FragmentBean yourBean) {
        Map<String, Object> map = new HashMap<>();
        FragmentBean yourBean1 = yourBeanService.addone(yourBean);
        if (yourBean1 != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", yourBean1);
            map.put("code", 0);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
            map.put("code", 1);
        }
        return map;
    }
    // 点赞
    @RequestMapping("/fragmentBean/setGoods")
    public Map<String, Object> setGoods(@RequestBody FragmentBean yourBean) {
        Map<String, Object> map = new HashMap<>();
        FragmentBean yourBean1 = yourBeanService.setGoods(yourBean);
        if (yourBean1 != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", yourBean1);
            map.put("code", 0);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
            map.put("code", 1);
        }
        return map;
    }
}

